/**
 * 手写useReducer
 */

function useReducer(reducer={},initialState={}) {
    const [state,useState]=useState(initialState);

    const dispatch = (action)=>{
        setState(reducer(state,action))
    }

    return [state,dispatch]
}

/**
 * useReducer 使用的 Reducer
 * @param state
 * @param action
 * @returns {(*&{count: *})|(*&{count: number})|*}
 */
function  reducer(state,action){
    switch (action.type){
        case 'ADD':{
            return {...state,count:state.count+1}
        }
        case 'SUB':{
            return  {...state,count:state.count -1}
        }
        default:{
            return  state
        }
    }
}
